package org.ourgrid.acceptance.discoveryservice;

import java.util.LinkedList;
import java.util.List;

import org.junit.Test;
import org.ourgrid.acceptance.util.discoveryservice.Req_502_Util;
import org.ourgrid.acceptance.util.discoveryservice.Req_504_Util;
import org.ourgrid.acceptance.util.discoveryservice.Req_505_Util;
import org.ourgrid.discoveryservice.DiscoveryServiceComponent;
import org.ourgrid.peer.PeerConstants;
import org.ourgrid.reqtrace.ReqTest;

import br.edu.ufcg.lsd.commune.functionaltests.util.TestStub;
import br.edu.ufcg.lsd.commune.identification.ServiceID;


public class Req_505_Test extends DiscoveryServiceAcceptanceTestCase {

	Req_502_Util req_502_util = new Req_502_Util(super.getComponentContext());
	Req_505_Util req_505_util = new Req_505_Util(super.getComponentContext());
	Req_504_Util req_504_util = new Req_504_Util(super.getComponentContext());

	/**
	 * Create a DS;
	 * Start a DS with the correct public key;
	 * Call the joinCommunity message with the following parameters:
     *    o WorkerProvider: username = test, servername = servertest and service = REMOTE_WORKERPROVIDER;
     *    o PeerStatusProvider: username = test, servername = servertest and service = PEER;
	 * Call the getCompleteStatus message;
	 * Verify if the conectedPeers list contains a the peer added.
	 *
	 * @throws Exception
	 */
	@ReqTest(test = "AT-505.1", reqs = "")
	@Test public void test_AT_505_1_AddAPeerToTheComunity() throws Exception{
		//create and start a DS
		DiscoveryServiceComponent component = req_502_util.startDiscoveryService();
		
		//create a WorkerProvider
		TestStub dscTestStub = req_505_util.createDiscoveryServiceClient(new ServiceID("test", "servertest", 
				PeerConstants.MODULE_NAME, PeerConstants.REMOTE_ACCESS_OBJECT_NAME));
		
		//Call joinCommunity
		req_505_util.getRemoteWorkerProviders(component, dscTestStub);
		
		//Call getCompletStatusMessage
		List<String> dscServiceIDList = new LinkedList<String>();
		dscServiceIDList.add(dscTestStub.getDeploymentID().getContainerID().getUserAtServer());
		req_504_util.getCompleteStatus(component, dscServiceIDList);
	}
	
	/**
	*  Create a DS;
    * Start a DS with the correct public key;
    * Call the joinCommunity message with the following parameters:
          o WorkerProvider: username = test, servername = servertest and service = REMOTE_WORKERPROVIDER;
          o PeerStatusProvider: username = test, servername = servertest and service = PEER;
    * Call the getCompleteStatus message;
    * Verify if the conectedPeers list contains a the peer added.
    * Call again the joinCommunity with the above parameters;
    * Verify if the following warn message was logged:
    * 	The client with ID [serviceID] is already added.
    * Call the getCompleteStatus message;
    * Verify if the conectedPeers list contains only the peer added

	 */
	@ReqTest(test = "AT-505.2", reqs = "")
	@Test public void test_AT_505_2_AddAPeerToTheComunityAgain() throws Exception{
		//create and start a DS
		DiscoveryServiceComponent component = req_502_util.startDiscoveryService();
		
		//create a WorkerProvider
		TestStub dscTestStub = req_505_util.createDiscoveryServiceClient(new ServiceID("test", "servertest", "PEER", "REMOTE_WORKERPROVIDER"));
		
		//Call joinCommunity
		req_505_util.getRemoteWorkerProviders(component, dscTestStub);
		
		//Call getCompletStatusMessage
		List<String> dscServiceIDList = new LinkedList<String>();
		dscServiceIDList.add(dscTestStub.getDeploymentID().getContainerID().getUserAtServer());
		req_504_util.getCompleteStatus(component, dscServiceIDList);
		
		//Call joinCommunity
		req_505_util.getRemoteWorkerProviders(component, dscTestStub, true);
		
		//Call getCompletStatusMessage
		req_504_util.getCompleteStatus(component, dscServiceIDList);
	}
	
	@ReqTest(test = "AT-505.3", reqs = "")
	@Test public void test_AT_505_3_AddAPeerToTheComunityAndShutItDown() throws Exception{
		//create and start a DS
		DiscoveryServiceComponent component = req_502_util.startDiscoveryService();
		
		//create a WorkerProvider
		TestStub dscTestStub = req_505_util.createDiscoveryServiceClient(new ServiceID("test", "servertest", "PEER", "REMOTE_WORKERPROVIDER"));
		
		//Call joinCommunity
		req_505_util.getRemoteWorkerProviders(component, dscTestStub);
		
		//Call getCompletStatusMessage
		List<String> dscServiceIDList = new LinkedList<String>();
		dscServiceIDList.add(dscTestStub.getDeploymentID().getContainerID().getUserAtServer());
		req_504_util.getCompleteStatus(component, dscServiceIDList);

		//Notify RWP failure
		req_505_util.notifyDiscoveryServiceClientFailure(component, dscTestStub);
		
		//Call getCompletStatusMessage
		req_504_util.getCompleteStatus(component, new LinkedList<String>());
	}
}
